package com.wkh.project.service.config.redis;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @author wangkehua
 * redisson配置信息
 */
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
public class RedisssonConfig {
    private String host;
    private String port;
    private String password;


    private int timeout = 3000;
    private static String ADDRESS_PREFIX = "redis://";

    /**
     * 自动装配
     * @return
     */
    @Bean
    RedissonClient redissonSingle(){
        Config config = new Config();
        if(!StringUtils.hasText(host)){
            throw new RuntimeException("host is  empty");
        }
        SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress(ADDRESS_PREFIX + host + ":" + port)
                .setTimeout(timeout);
        if(StringUtils.hasText(this.password)) {
            singleServerConfig.setPassword(this.password);
        }
        return Redisson.create(config);
    }
}
